package com.tom.service.impl;

import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tom.util.HttpRequest;
import com.tom.util.JDBCUtil;

public class XinBoDai {
	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月8日
	 * @TODO 获取新博贷url
	 */
	public static void work(){
		ArrayList<String> list = new ArrayList<String>();
		String url = "https://www.xinbodai.com/p2p_sy_tbxm_servlet";
		for (int i = 0; i < 14; i++) {
			String param = "flag=gd&pageCont=";
			param += i+"&borrow_jklb=&borrow_jkqx=&borrow_nhll=";
			String result = HttpRequest.sentPost(url, param);
			//System.out.println(result);
			JSONArray jArray = JSONArray.parseArray(result);
			for (int j = 1; j < jArray.size(); j++) {
				JSONObject object = jArray.getJSONObject(j);
				if (object.getString("borrow_zt").equals("4")) {
					String borrow_id = object.getString("borrow_id");
					System.out.println(borrow_id);
					String Url = borrow_id;
					list.add(Url);
				}
			}
		}
		//添加数据库操作
		/*try {
			JDBCUtil.insertUrls(list,"qss_xinbodai_url");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}

	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月8日
	 * @TODO 获取新博贷业务json数据
	 */
	public static void work2(){
		String url [] = {"https://www.xinbodai.com/p2p_wytz_servlet","https://www.xinbodai.com/p2p_wytz_list_servlet"};
		ArrayList<String> list = new ArrayList<String>();
		ArrayList<String>  dArrayList = new ArrayList<String>();
		try {
			list = JDBCUtil.getUrls("qss_xinbodai_url");
			for (int i = 0; i < list.size(); i++) {
				String id = list.get(i);
				String param = "flag=Get_iddata&borrow_id=";
				param += id;
				String result = HttpRequest.sentPost(url[0], param);
				param = "flag=Get_tzxx&borrow_id=";
				param += id;
				String result1 = HttpRequest.sentPost(url[1], param);
				//System.out.println(result1);
				JSONArray jsonArray = JSONArray.parseArray(result1);
				result1 = (String) jsonArray.get(0);
				Date now = new Date(); 
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				String time = dateFormat.format(now);
				String sql = "insert into qss_xinbodai_itemdata(json,json1,insertdate) values("+"'"+result+"'"+","+"'"+result1+"'"+","+"'"+time+"'"+")"; 
				dArrayList.add(sql);
				System.out.println("-----------------"+i+i+i+i+i+"--------------------");
			}
			//添加数据库操作
			//JDBCUtil.insertData(dArrayList);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月8日
	 * @TODO 处理业务json数据并入库
	 */
	public static void work3(){
		try {
			ArrayList<HashMap<String, String>> list = JDBCUtil.getJsonData("json","qss_xinbodai_itemdata");
			ArrayList<HashMap<String, String>> list1 = JDBCUtil.getJsonData("json1","qss_xinbodai_itemdata");
			ArrayList<String> result = new ArrayList<String>();
			for (int i = 0; i < list.size(); i++) {
				HashMap<String, String> map = list.get(i);
				HashMap<String, String> map1 = list1.get(i);
				//System.out.println(map);
				String json = map.get("json");
				String regEx_html = "<[^>]+>";
				Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);  
			    Matcher m_html = p_html.matcher(json);
			    json = m_html.replaceAll("");
				JSONArray array = JSONArray.parseArray(json);
				JSONObject object = array.getJSONObject(0);
				//System.out.println(object);
				String borrow_name = object.getString("borrow_name");
				String borrow_nhll = object.getString("borrow_nhll");
				String jkqx = object.getString("jkqx");
				if (jkqx.equals("天标")) {
					jkqx = object.getString("borrow_tbts")+"天";
				}
				String borrow_bj = object.getString("borrow_bj");
				String borrow_tbcs = object.getString("borrow_tbcs");
				String borrow_fbshsj = object.getString("borrow_fbshsj");
				String borrow_tbje = object.getString("borrow_tbje");
				double xmjd = Math.floor(Double.parseDouble(borrow_tbje)/Double.parseDouble(borrow_bj)*10000)/10000;
				String percent = new DecimalFormat("#.00").format(xmjd*100) +"%";
				String hkfs = object.getString("hkfs");
				String xmlx = "";
				if (object.getString("borrow_dbbbz").equals("1")) {
					xmlx = "担保标";
				} 
				if (object.getString("borrow_xsbz").equals("1")) {
					xmlx = "新手标";
				} 
				if (object.getString("borrow_vipbz").equals("1")) {
					xmlx = "VIP标";
				}
				if (object.getString("borrow_tybbz").equals("1")) {
					xmlx = "体验标";
				} 
				if (object.getString("borrow_fastbz").equals("1")) {
					xmlx = "低压标";
				} 
				if (object.getString("borrow_jzbbz").equals("1")) {
					xmlx = "净值标";
				} 
				if (object.getString("borrow_xybbz").equals("1")) {
					xmlx = "信用标";
				}
				if(object.getString("borrow_mbbz").equals("1")) {
					xmlx = "秒还标";
				}
				String borrow_jkxq = object.getString("borrow_jkxq");
				//System.out.println(borrow_jkxq);
				
				//---------------投资信息---------------
				String json1 = map1.get("json1");
				//System.out.println(json1);
				String tzxx = "";
				JSONArray array2 = JSONArray.parseArray(json1);
				for (int j = 0; j < array2.size(); j++) {
					JSONObject object2 = array2.getJSONObject(j);
					//System.out.println(object2);
					String user_username = object2.getString("user_username"); //名称
					String tender_tbbj = object2.getString("tender_tbbj");   //金额
					String tender_tjsj = object2.getString("tender_tjsj"); // 时间
					String tender_dslx = object2.getString("tender_dslx"); // 收益
					String tender_zt = object2.getString("tender_zt"); //状态 0:成功
					tzxx += user_username+"##"+tender_tbbj+"##"+tender_tjsj+"##"+tender_dslx+"##"+tender_zt+"==";
				}
				tzxx = tzxx.substring(0, tzxx.length()-2);
				//System.out.println(tzxx);
				Date now = new Date(); 
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				String time = dateFormat.format(now);
				String sql = "insert into qss_xinbodai_bussiness_data(borrow_name,borrow_nhll,jkqx,borrow_bj,borrow_tbcs,borrow_fbshsj,percent,hkfs,xmlx,borrow_jkxq,tzxx,insertdate)"
						+ " values("+"'"+borrow_name+"'"+","+"'"+borrow_nhll+"'"+","+"'"+jkqx+"'"+","+"'"+borrow_bj+"'"+","+"'"+borrow_tbcs+"'"+","+"'"+borrow_fbshsj+"'"+","
								+ ""+"'"+percent+"'"+","+"'"+hkfs+"'"+","+"'"+xmlx+"'"+","+"'"+borrow_jkxq+"'"+","+"'"+tzxx+"'"+","+"'"+time+"'"+")";
				result.add(sql);
				System.out.println("--------------"+i+i+i+i+i+i+"------------------");
			}
			//添加数据库操作
			//JDBCUtil.insertData(result);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
